<?php
namespace App\Api;

use PhalApi\Api;
use App\Domain\User as UserDomain;
use App\Domain\File as FileDomain;
use App\Domain\Order as OrderDomain;

/**
 * 用户中心模块接口服务
 */
class User extends Api {
    public function getRules() {
        return array(
            'userInfo' => array(
            ),
            'uplodFile' => array(
                'file' => array(
                    'name' => 'file',        // 客户端上传的文件字段
                    'type' => 'file',
                    'require' => true,
                    'max' => 2097152,        // 最大允许上传2M = 2 * 1024 * 1024,
                    'range' => array('image/jpg','image/jpeg','image/png','image/gif'),  // 允许的文件格式
                    'ext' => array('jpg','jpeg','png','gif' ),// 允许的文件扩展名
                    'desc' => '待上传的文件',
                ),
            ),
            'updateUser'=> array(
                'member_mobile' => array('name' => 'member_mobile', 'require' => false, 'desc' => '用户绑定的手机号'),
                'member_name' => array('name' => 'member_name', 'require' => false, 'desc' => '用户姓名'),
                'member_avatar' => array('name' => 'member_avatar', 'require' => false, 'desc' => '用户头像'),
//                    'province' => array('name' => 'province', 'require' => true, 'desc' => '省ID'),
//                    'city' => array('name' => 'city', 'require' => true, 'desc' => '市ID'),
//                    'district' => array('name' => 'district', 'require' => true, 'desc' => '区ID'),
                'region' => array('name' => 'region', 'require' => false, 'desc' => '城市'),
                'address' => array('name' => 'address', 'require' => false, 'desc' => '收货地址'),
                'member_birthday' => array('name' => 'member_birthday', 'require' => false, 'desc' => '生日'),
                'member_sex' => array('name' => 'member_sex', 'require' => false, 'desc' => '性别 1男 2女'),
            ),
            'orderList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'default' => 1, 'min' => 1, 'desc' => \PhalApi\T('page')),
                'limit' => array('name' => 'limit', 'type' => 'int', 'default' => 20, 'min' => 1, 'max' => 1000, 'desc' => \PhalApi\T('page limit')),
                'order_status' => array('name' => 'order_status', 'require' => false, 'desc' => '订单状态 00待支付 10 待签收 20 已签收 默认为所有竞价过的列表数据'),
            ),
        );
    }
    /**
     * 用户信息详情接口
     * @desc 用户信息详情接口
     * @return int user_id 用户ID
     * @return string member_mobile 用户绑定的手机号
     * @return string member_name 用户姓名
     * @return url member_avatar 用户头像
     * @return int province 省ID
     * @return int city 市ID
     * @return int district 区ID
     * @return string region 地区描述
     * @return string address 收货地址
     * @return string member_birthday 生日
     * @return int member_sex 性别 1男 2女
     * @return string add_time 注册时间
     */
    public function userInfo() {
        return \PhalApi\DI()->app->check();
    }

    /**
     * 头像上传接口
     * @desc 头像上传接口
     * @return url url 图片地址
     * @return string fileName 图片编码
     * @return string oldFileName 图片名称
     */
    public function uplodFile(){
        $domain = new UserDomain();
        $fileInfo=$this->file;
        return $domain->uplodAvatar($fileInfo);
    }

    /**
     * 更新用户信息
     * @desc 更新用户信息
     */
    public function updateUser() {
        $domain = new UserDomain();
        //列出所有需要添加的参数
        $updateData = array();
        if( isset($this->member_mobile) && $this->member_mobile != '' ){
            $updateData['member_mobile'] = $this->member_mobile;
        }
        if( isset($this->member_name) && $this->member_name != '' ){
            $updateData['member_name'] = $this->member_name;
        }
        if( isset($this->member_avatar) && $this->member_avatar != '' ){
            $updateData['member_avatar'] = $this->member_avatar;
        }
        if( isset($this->region) && $this->region != '' ){
            $updateData['region'] = $this->region;
        }
//        $updateData['province'] = $this->province;
//        $updateData['city'] = $this->city;
//        $updateData['district'] = $this->district;
        if( isset($this->address) && $this->address != '' ){
            $updateData['address'] = $this->address;
        }
        if( isset($this->member_birthday) && $this->member_birthday != '' ){
            $updateData['member_birthday'] = $this->member_birthday;
        }
        if( isset($this->member_sex) && $this->member_sex != '' ){
            $updateData['member_sex'] = $this->member_sex;
        }
        return $domain->updateUser($updateData);
    }

    /**
     * 获取订单列表
     * @desc 获取订单列表
     * @return int total 总数
     * @return array items 数据列表
     * @return int items->id 订单id
     * @return string items->member_name 出价人姓名
     * @return string items->member_mobile 出价人电话
     * @return float items->order_price 出价价格
     * @return string items->order_no 订单编号
     * @return string items->order_status 订单状态
     * @return string items->add_time 出价时间
     * @return int items->goods_id 商品id
     * @return string items->goods_name 商品名称
     * @return string items->goods_imgs 商品图片
     * @return string items->store_name 专场名称
     */
    public function orderList() {
        $domain = new OrderDomain();
        $where = array();
        //分页
        $page = $this->page? $this->page:1;
        $perpage = $this->limit?$this->limit:10;
        //要查询的字段
        $select="so.id,so.member_name,so.member_mobile,so.order_price,so.order_no,so.order_status,from_unixtime(so.add_time, '%Y-%m-%d %H:%i:%S') as add_time,sg.id as goods_id,sg.goods_imgs,sg.goods_name,sg.level_num,ss.store_name";
        //处理条件查询
        if( isset($this->order_status)&&$this->order_status != '' ){
            $where['so.order_status'] = $this->order_status;
        }
        $order='so.id DESC';
        //处理排序
        if( isset($this->sort) ){
            if( $this->sort == '+id' ){
                $order='so.id ASC';
            }elseif($this->sort == '-id'){
                $order='so.id DESC';
            }
        }
        $roles = $domain->orderList($where,$select,$order,$page,$perpage);
        return $roles;
    }
}
